package com.htrq.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class BaseDaoImpl<T> implements BaseDao<T> {

 /*   @Autowired
    protected HttpServletRequest request;*/
	@Resource(name="sessionFactory")
    protected SessionFactory     sessionFactory;
    
	@Resource(name="sersessionFactory")
    protected SessionFactory     sersessionFactory;
    
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    public void setSersessionFactory(SessionFactory sessionFactory) {
        this.sersessionFactory = sessionFactory;
    }
    
    public Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }
    
    public Serializable add(T t) {
        return getCurrentSession().save(t);
    };

    public void update(T t) {
        getCurrentSession().update(t);
    };

    public void saveOrUpdate(T t) {
        getCurrentSession().saveOrUpdate(t);
    };

    @SuppressWarnings("unchecked")
	public T get(Serializable id) {
        return (T) this.getCurrentSession().get((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0],id);
    };

    public void delete(T t) {
        this.getCurrentSession().delete(t);
    };

    @SuppressWarnings("unchecked")
	public List<T> findByHql(String hql) {
        Query query = this.getCurrentSession().createQuery(hql);
        return query.list();
    };

    @SuppressWarnings("unchecked")
	public List<T> findByHql(String hql, Object... values) {
        Query query = this.getCurrentSession().createQuery(hql);
        for (int i = 0; i < values.length; i++)
            query.setParameter(i, values[i]);
        return query.list();
    }

    @SuppressWarnings("unchecked")
	public List<T> findByNativeSql(String sql) {
        Session session = this.getCurrentSession();
        SQLQuery query = session.createSQLQuery(sql);
        List<T> result = query.list();
        return result;
    }

    /*
     * public List<T> queryByPage(Page page) { Session session=this.getCurrentSession(); Class < T > entityClass =
     * (Class < T > ) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[ 0 ]; String
     * tString=entityClass.getSimpleName(); Query query=session.createQuery("From "+tString);
     * query.setFirstResult(page.getStart()); query.setMaxResults(page.getLimit()); List<T> ts=query.list(); return ts;
     * }
     */
    public void excuteSql(String sql) {
        getCurrentSession().createSQLQuery(sql).executeUpdate();

    }

    @SuppressWarnings("unchecked")
	public List<T> findAll(String hql) {
        Query query = this.getCurrentSession().createQuery(hql);
        return query.list();
    }
    
    public void delete(Collection<T> ts) {

    }
    
    public Long getCount(String hql){
    	 Query query = this.getCurrentSession().createQuery(hql);
    	 return (Long) query.uniqueResult();
    }
}

